<?php

require_once (fvSite::$fvConfig->get("path.entity") . 'staticpages.class.php') ;

class StaticPagesManager extends fvRootManager {
	
	protected function __construct () {
	    $objectClassName = substr(__CLASS__, 0, -7);
	    
	    // Tweak for ManagerManager Class ;)
	    if ($objectClassName == "") $objectClassName = "Manager";
	    
	    $this->rootObj = new $objectClassName();
	}
	
    static function getInstance()
    {
        static $instance; 
        
        $className = __CLASS__;
        
        if (!isset($instance)) {
            $instance = new $className();
        }  
        return $instance;
    }
    
    function getTree($where = null, $args = array()) {
    	$query = "SELECT q.* FROM " . $this->rootObj->getTableName() . " q
    left join " . $this->rootObj->getTableName() . " q_p on q_p.id = q.parent";
    	if (!empty($where)) $query .= "\nWHERE (" . $where . ")";
    	$query .= "\norder by q.id, q.parent, IF (q.parent = 0, (q.id + 1)*100000, (q_p.id + 1)*100000 + (q.id + 1)) ASC";
    	$res = fvSite::$DB->query($query, $params);
    
    	$result = array();
    
    	if (!DB::isError($res)) {
    		while ($row = $res->fetchRow()) {
    			$o = clone $this->rootObj;
    			$o->hydrate($row);
    			$result[] = $o;
    		}
    	}
    
    	return $result;
    }
}
